package cn.edu.zut.sunday.cve.controller;


import cn.edu.zut.sunday.base.controller.GenericController;
import cn.edu.zut.sunday.cve.domain.InfoData;
import cn.edu.zut.sunday.cve.service.impl.InfoDataService;

import cn.edu.zut.sunday.misc.vo.InformationGroupQuery;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.Predicate;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Api("cve-信息安全数据接口")
@Slf4j
@RestController
@RequestMapping("cve/infodata")
@CrossOrigin
public class InfoDataController extends GenericController<InfoData, Integer, InfoDataService> {
    @Autowired
    public void setInfoDataService(InfoDataService infoDataService) {
        this.service = infoDataService;
    }

    @Override
    protected Pageable createPageable(int page, int size) {
        Sort sort = Sort.by(Sort.Direction.DESC, "published");
        return PageRequest.of(page, size, sort);


    }


    @Override
    protected Specification<InfoData> createSpecification(String requestBodyStr) {
        log.info("requestBodyStr: {}", requestBodyStr);
        final InformationGroupQuery informationGroupQuery = new Gson().fromJson(requestBodyStr,
                InformationGroupQuery.class);
        log.info("InformationQuery: {}", informationGroupQuery);

        if (informationGroupQuery != null) {
            return getInformationGroupSpecification(informationGroupQuery);
        }
        return null;
    }

    //分页查询
    private Specification<InfoData> getInformationGroupSpecification(
            InformationGroupQuery informationGroupQuery) {
        String cveId = informationGroupQuery.getCveId();
        String vulnType = informationGroupQuery.getVulnType();
        Specification<InfoData> specification = (root, query, criteriaBuilder) -> {
            query.distinct(true);
            List<Predicate> list = new ArrayList<>();

            if (!StringUtils.isEmpty(cveId)) {
                Predicate p1 = criteriaBuilder.like(root.get("cveId"), sqlLike(cveId));
                list.add(p1);
            }
            if (!StringUtils.isEmpty(vulnType)) {
                // 此处为查询serverName中含有key的数据
                Predicate p2 = criteriaBuilder.like(root.get("vulnType"), sqlLike(vulnType));
                list.add(p2);
            }

            return criteriaBuilder.and(list.toArray(new Predicate[0]));
        };
        return specification;
    }

    //增加方法
    @Override
    public InfoData add(@RequestBody InfoData infoData,
                        javax.servlet.http.HttpServletRequest request) {
        if (infoData== null) {
            return null;
        }
        infoData=setCommonAttr(infoData, true);
        infoData=this.service.save(infoData);
        return infoData;
    }
//修改方法

    @Override
    public InfoData  update(@PathVariable Integer id,
                            @RequestBody InfoData infoData, HttpServletRequest request) {
        infoData.setId(id);
        infoData=setCommonAttr(infoData, false);
        infoData=this.service.save(infoData);
        return infoData;

    }
    //删除方法
    public InfoData findById(@PathVariable Integer id) {

        return this.service.findById(id);
    }


}


